﻿//using MediatR;
using MediatR;
using MyMES.Domain.zmc;
using MyMES.Error;
using MyMES.Infrastructure;
using MyMES.Infrastructure.Interface;
using MyMES.Read.API.Application.Commends.Proces;
using System.Drawing.Printing;


namespace MyMES.Read.API.Application.CommendHandlers.Proces
{
    public class GetProcessCommendHandlers : IRequestHandler<GetProcessCommends, APIPaging<List<Process>>>
    {

        private readonly IBaseRepository<Process> processRepository;



        public GetProcessCommendHandlers(IBaseRepository<Process> processRepository)
        {
            this.processRepository = processRepository;
        }

        /// <summary>
        /// 显示工艺路线
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIPaging<List<Process>>> Handle(GetProcessCommends request, CancellationToken cancellationToken)
        {
            try
            {
                APIPaging<List<Process>> result = new APIPaging<List<Process>>();

                result.Code = APIEnum.查询成功;
                result.Msg = "查询成功";

                string sql = "select * from Process    where IsDel=false   ORDER BY ProcessID DESC  ";

                var list = await processRepository.SelectAsync(sql);
                if (!string.IsNullOrEmpty(request.ProcessNo))
                {
                    list = list.Where(x => x.ProcessNo.Contains(request.ProcessNo)).ToList();
                }
                if (!string.IsNullOrEmpty(request.ProcessName))
                {
                    list = list.Where(x => x.ProcessName.Contains(request.ProcessName)).ToList();
                }
                if (request.isvailable != null)
                {
                    var isvailable = request.isvailable == 1 ? true : false;
                    list = list.Where(x => x.isvailable == isvailable).ToList();

                }
                result.TotalCount = list.Count;
                result.TotalPage = (int)Math.Ceiling(result.TotalCount * 1.0 / request.PageSize);
                list = list.Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();

                result.data = list;
                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
